package com.softstao.guoyu.ui.activity.me;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.me.AuditInfo;
import com.softstao.guoyu.model.me.Level;
import com.softstao.guoyu.model.me.User;
import com.softstao.guoyu.mvp.interactor.me.AuditInteractor;
import com.softstao.guoyu.mvp.presenter.me.AuditPresenter;
import com.softstao.guoyu.mvp.viewer.me.AuditInfoViewer;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;

/**
 * Created by vaikus on 2017/6/23.
 */

public class AuditHistoryActivity extends BaseActivity implements AuditInfoViewer {

    @AIPresenter(presenter = AuditPresenter.class, interactor = AuditInteractor.class)
    AuditPresenter presenter;

    @BindView(R.id.current_level)
    TextView currentLevel;
    @BindView(R.id.level_view)
    RecyclerView levelView;
    private String level_name;
    private AuditInfo auditInfo;
    private static List<Level> levelList = new ArrayList<>();
    private RecycleViewBaseAdapter<Level> adapter;
    private static Integer[] ids = new Integer[]{1,2,3,4,5};
    private static String[] nameList = new String[]{"天使经销商", "皇冠经销商", "一级经销商", "市级经销商", "省级经销商"};
    private static String[] memoList = new String[]{"需要一次性缴纳首次款：1200元，无需保证金。",
                                            "需要一次性缴纳首次款：6800元，无需保证金。",
                                            "1.需要一次性缴纳首次款：27000元\n2.提交保证金：1000元",
                                            "1.需要一次性缴纳首次款：85000元\n2.提交保证金：3000元",
                                            "1.需要一次性缴纳首次款：365000元\n2.提交保证金：15000元"};
    private static String[] bondList = new String[]{"0.00元","0.00元","1000.00元","3000.00元","15000.00元"};

    @Override
    public int _ContentView() {
        return R.layout.activity_audit_history;
    }

    @Override
    public void initView() {
        initTitle("授权记录");

        adapter = new RecycleViewBaseAdapter<Level>(levelList,R.layout.layout_level_item) {
            @Override
            public void convert(RecycleViewHolder holder, Level level) {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                holder.itemView.setLayoutParams(layoutParams);
                holder.setText(R.id.name, level.getName())
                        .setText(R.id.memo, level.getMemo());
                TextView btn = holder.getView(R.id.button);
                ImageView audited = holder.getView(R.id.audited_view);
                if (auditInfo != null) {
                    if (auditInfo.getCurrentLevel() == level.getId()) {
                        btn.setVisibility(View.VISIBLE);
                        audited.setVisibility(View.GONE);
                        btn.setText("我的授权证书");
                        btn.setTextColor(getResources().getColor(R.color.font_blue));
                        btn.setBackgroundResource(R.drawable.blue_border_line);
                        btn.setOnClickListener(v -> {
                            if (auditInfo.getCurrentLevelCertId() == -1) {
                                LZToast.getInstance(context).showToast("该代理暂无授权证书");
                            } else {
                                startActivity(new Intent(context, AuditCertActivity.class).putExtra("id",auditInfo.getCurrentLevelCertId()));
                            }
                        });
                    } else {
                        if (level.getId() > auditInfo.getCurrentLevel() || auditInfo.getCurrentLevel() == 7) {
                            btn.setVisibility(View.VISIBLE);
                            audited.setVisibility(View.GONE);
                            btn.setText("我要升级");
                            btn.setTextColor(getResources().getColor(R.color.font_blue));
                            btn.setBackgroundResource(R.drawable.blue_border_line);
                            btn.setOnClickListener(v -> {
                                if (auditInfo.getProcessLevel()==0){
                                    Intent intent = new Intent(context, AuditAddActivity.class);
                                    intent.putExtra("level", level);
                                    startActivity(intent);
                                }else {
                                    LZToast.getInstance(context).showToast("您已有审核中的升级申请");
                                }
                            });
                            if (auditInfo.getProcessId() != 0) {
                                if (level.getId() == auditInfo.getProcessLevel()) {
                                    btn.setTextColor(getResources().getColor(R.color.font_pink));
                                    btn.setBackgroundResource(R.drawable.pink_border_line);
                                    if (auditInfo.getProcessState() == 0) {
                                        btn.setText("审核中");
                                        btn.setOnClickListener(v -> {
                                            Intent intent = new Intent(context, AuditDetailActivity.class);
                                            intent.putExtra("type", "user");
                                            intent.putExtra("id", auditInfo.getProcessId());
                                            startActivity(intent);
                                        });
                                    } else if (auditInfo.getProcessState() == 1) {
                                        btn.setText("已被驳回");
                                        btn.setOnClickListener(v -> {
                                            Intent intent = new Intent(context, AuditDetailActivity.class);
                                            intent.putExtra("type", "user");
                                            intent.putExtra("id", auditInfo.getProcessId());
                                            intent.putExtra("flag",1);
                                            startActivity(intent);
                                        });
                                    }
                                }
                            }
                        } else {
                            btn.setVisibility(View.GONE);
                            audited.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }
        };
        levelView.setAdapter(adapter);
        levelView.setLayoutManager(new FullyLinearLayoutManager(this));
    }

    private void initData(){
        switch (User.Type.fromInteger(auditInfo.getCurrentLevel())){
            case ANGEL:
                level_name = "天使";
                break;
            case CROWN:
                level_name = "皇冠";
                break;
            case ONE_LEVEL:
                level_name = "一级";
                break;
            case CITY_LEVEL:
                level_name = "市级";
                break;
            case PROVINCE:
                level_name = "省代";
                break;
            case HEADQUARTER:
                level_name = "总部";
                break;
            case UNAGENT:
                level_name = "未授权";
                break;
        }
        currentLevel.setText(level_name);

        setLevel(levelList);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAuditInfo();
    }

    public List<Level> getLevel() {
        return levelList;
    }

    public static void setLevel(List<Level> levelList) {
        levelList.clear();
        for (int i=0; i<ids.length; i++){
            Level level = new Level();
            level.setId(ids[i]);
            level.setName(nameList[i]);
            level.setMemo(memoList[i]);
            level.setBond(bondList[i]);
            levelList.add(level);
        }
        Collections.reverse(levelList);
    }

    @Override
    public void getAuditInfo() {
        presenter.getAuditInfo(SharePreferenceManager.getInstance().getAgentId());
//        presenter.getAuditInfo(5);
    }

    @Override
    public void getResult(AuditInfo auditInfo) {
        if (auditInfo!=null){
            this.auditInfo = auditInfo;
            initData();
        }
    }
}
